﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ECGadmin.Common;
using ECGadmin.DAL;
using ECGadmin.WebUI.Attributes;
using ECGadmin.WebUI.CustomHelpers;
using ECGadmin.WebUI.Models;

namespace ECGadmin.WebUI.Controllers
{
    [AdminLoggedAuthorize]
    public class SizeController : Controller
    {
        //
        // GET: /Admin/Size/

        public ActionResult Index(int page = 1)
        {
            var itemsPerPage = 10;
            var totalPage = 0;
            var model = SizeDb.GetAllPaging(page, itemsPerPage, ref totalPage) ?? new List<SizeListDto>();
            ViewBag.PageInfo = new PageInfo {CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage};
            return View(model);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(SizeDto model)
        {
            model.Id = FunctionHelper.GenerateId();
            model.CreateDate = DateTime.Now;
            SizeDb.Add(model);
            return RedirectToAction("Index");
        }

        public ActionResult Edit(string id)
        {
            var model = SizeDb.GetById(id) ?? new SizeDto();
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(SizeDto model)
        {
            SizeDb.Update(model);
            return View(model);
        }

        public JsonResult DeleteSize(string id)
        {
            var result = SizeDb.DeleteSize(id);
            return Json(result > 0, JsonRequestBehavior.AllowGet);
        }
    }
}
